﻿using BCCommon;
using BCCommon.Enums;
using BCEntity.Website.User;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace BCData.Website.User.UserAuth
{
    /// <summary>
    /// 用户授权数据
    /// </summary>
    public class UserAuthData : IUserAuthData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM userauth";
        private const string QUERY = @"SELECT UserAuthId,UserId,AuthType,UnionId,AuthInfo FROM UserAuth";

        public UserAuthData(IDatabaseContext databaseContext)
        {
            this.databaseContext = databaseContext;
        }

        public async Task<bool> Exists(long userAuthId)
        {
            string sql = string.Format(@"{0} WHERE UserAuthId=?UserAuthId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserAuthId", userAuthId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<bool> Exists(AuthType authType, string unionId)
        {
            string sql = string.Format(@"{0} WHERE AuthType=?AuthType AND UnionId=?UnionId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?AuthType", authType.ToString(), DbType.AnsiString);
            parameters.Add("?UnionId", unionId, DbType.AnsiString);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<UserAuthEntity> Add(UserAuthEntity userAuthEntity)
        {
            string sql = @"INSERT INTO UserAuth(UserId,AuthType,UnionId,AuthInfo)VALUES(?UserId,?AuthType,?UnionId,?AuthInfo);
                            {0} WHERE UserAuthId=LAST_INSERT_ID()";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserId", userAuthEntity.UserId, DbType.Int64, size: 20);
            parameters.Add("?AuthType", userAuthEntity.AuthType.ToString(), DbType.AnsiString);
            parameters.Add("?UnionId", userAuthEntity.UnionId, DbType.AnsiString, size: 50);
            parameters.Add("?AuthInfo", userAuthEntity.AuthInfo, DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<UserAuthEntity>(string.Format(sql, QUERY), parameters, databaseContext.Transaction);
        }

        public async Task<bool> CheckUserAuthExists(long userId, AuthType authType, string unionId)
        {
            string sql = @"SELECT 1 FROM UserAuth WHERE UserId=?UserId AND AuthType=?AuthType AND UnionId=?UnionId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserId", userId, DbType.Int64, size: 20);
            parameters.Add("?AuthType", authType.ToString(), DbType.AnsiString);
            parameters.Add("?UnionId", unionId, DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters)!=null;
        }

        public void Delete(long userAuthId)
        {
            string sql = @"DELETE FROM UserAuth WHERE UserAuthId=?UserAuthId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserAuthId", userAuthId, DbType.Int64, size: 20);
            this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction);
        }

        public async Task<UserAuthEntity> Get(string unionId)
        {
            string sql = @"{0} WHERE UnionId=?UnionId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UnionId", unionId, DbType.AnsiString, size: 50);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<UserAuthEntity>(string.Format(sql, QUERY), parameters);
        }

        public async Task<UserAuthEntity> Get(long userId)
        {
            string sql = @"{0} WHERE UserId=?UserId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserId", userId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<UserAuthEntity>(string.Format(sql, QUERY), parameters);
        }

        public async Task<UserAuthEntity> Get(long userId, string authType)
        {
            string sql = @"{0} WHERE UserId=?UserId AND AuthType=?AuthType;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserId", userId, DbType.Int64, size: 20);
            parameters.Add("?AuthType", authType);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<UserAuthEntity>(string.Format(sql, QUERY), parameters);
        }

        public async Task<UserAuthEntity> GetEntity(AuthType authType, string unionId)
        {
            string sql = string.Format(@"{0} WHERE AuthType=?AuthType AND UnionId=?UnionId LIMIT 1", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?AuthType", authType.ToString(), DbType.AnsiString);
            parameters.Add("?UnionId", unionId, DbType.AnsiString);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<UserAuthEntity>(sql, parameters);
        }

        public async Task<UserAuthEntity> Update(UserAuthEntity userAuthEntity)
        {
            string sql = @"UPDATE UserAuth SET
                                UserId=?UserId,
                                AuthType=?AuthType,
                                UnionId=?UnionId,
                                AuthInfo=?AuthInfo
                            WHERE UserAuthId=?UserAuthId;
                            {0} WHERE UserAuthId=?UserAuthId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserAuthId", userAuthEntity.UserAuthId, DbType.Int64, size: 20);
            parameters.Add("?UserId", userAuthEntity.UserId, DbType.Int64, size: 20);
            parameters.Add("?AuthType", userAuthEntity.AuthType.ToString(), DbType.AnsiString);
            parameters.Add("?UnionId", userAuthEntity.UnionId, DbType.AnsiString, size: 50);
            parameters.Add("?AuthInfo", userAuthEntity.AuthInfo, DbType.AnsiString, size: 1000);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<UserAuthEntity>(string.Format(sql, QUERY), parameters, databaseContext.Transaction);
        }

        #region 获取信息列表
        public async Task<IEnumerable<UserAuthEntity>> GetList(long userId)
        {
            string sql = string.Format(@"{0} WHERE UserId=?UserId;",QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UserId", userId, DbType.Int64, size: 20);
            return await this.databaseContext.Slave.QueryAsync<UserAuthEntity>(sql, parameters);
        }
        #endregion
    }
}
